package jbl.service.impl;

import java.util.ArrayList;
import java.util.List;

import jbl.dao.LbDaoI;
import jbl.model.Code_lb;
import jbl.pageModel.DataGrid;
import jbl.pageModel.Lb;
import jbl.service.LbServiceI;
import jbl.util.Encrypt;
import jbl.util.Json;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*
 * 用户权限菜单表
 * */
@Service("lbService")
public class LbServiceImpl implements LbServiceI{
	
	private static final Logger logger = Logger.getLogger(LbServiceImpl.class);

	
	private LbDaoI lbDao;
	
	@Autowired
	public void setLbDao(LbDaoI lbDao) {
		this.lbDao = lbDao;
	}


	@Override
	public DataGrid dataGrid() {
		DataGrid dg = new DataGrid();
		try {
			List<Lb> rl = new ArrayList<Lb>();
			String hql = "from Code_lb t ";
			List<Code_lb> nl = lbDao.find(hql);
			if(null != nl && nl.size() > 0){
				for(Code_lb t : nl){
					Lb lb = new Lb();
					BeanUtils.copyProperties(t, lb);
					rl.add(lb);
				}
				dg.setTotal((long)rl.size());
				dg.setRows(rl);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("查询lb表错误:"+e.getMessage());
		}
		return dg;
	}
	
	@Override
	public Json add(Lb lb){
		Json j = new Json();
		try {
			Code_lb code_lb = new Code_lb();
			
			String userid = Encrypt.createUUID();
			code_lb.setId(userid);
			code_lb.setLb(lb.getLb());

			lbDao.save(code_lb);
			j.setMsg("添加成功");
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			j.setMsg("添加失败");
		}
		return j;
	}
	
	@Override
	public Json edit(Lb lb){
		Json j = new Json();
		try {
			Code_lb code_lb = new Code_lb();
			BeanUtils.copyProperties(lb, code_lb);
			lbDao.update(code_lb);
			j.setMsg("修改成功");
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			j.setMsg("修改失败");
		}
		return j;
	}
	

	@Override
	public Json delete(String  ids) {
		Json j = new Json();
		try {
			if(ids != null){
				for (String id : ids.split(",")) {
					if(!id.trim().equals("")){
						Code_lb t = lbDao.get(Code_lb.class,id.trim());
						lbDao.delete(t);
					}
				}
			}
			j.setMsg("删除成功");
			j.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setMsg("删除失败");
		}
		return j;
	}
	@Override
	public List<Lb> comboboxInfo() {
		List<Lb> rl = new ArrayList<Lb>();
		try {
			
			String hql = "from Code_lb t ";
			List<Code_lb> nl  = lbDao.find(hql);
			if(null != nl && nl.size() > 0){
				for(Code_lb t : nl){
					Lb lb = new Lb();
					BeanUtils.copyProperties(t, lb);
					rl.add(lb);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("查询lb表错误:"+e.getMessage());
		}
		return rl;
	}

}











